package Student;

import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;

public class StudentDAOImpl implements StudentDAO {

    @Override
    public int insertstudent(Student student) {

        try(Connection connection = DBUtil.getConnection()) {
            String sql="INSERT INTO student(student_id,name,age,gender,class_name) " +
                    "VALUES (?,?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,student.getStudentId());
            preparedStatement.setString(2,student.getName());
            preparedStatement.setInt(3,student.getAge());
            preparedStatement.setString(4,student.getGender());
            preparedStatement.setString(5,student.getClassName());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("插入成功");
            }else{
                System.out.println("插入失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public int deletestudent(int studentId) {

        try(Connection connection = DBUtil.getConnection()) {
            String sql="DELETE FROM student WHERE student_id = ?;";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,studentId);
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public int updatestudent(Student student) {
        try(Connection connection = DBUtil.getConnection()) {
            String sql="UPDATE student SET student_id=? , name=?,gender=?,age=?,class_name=? WHERE id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,student.getStudentId());
            preparedStatement.setString(2,student.getName());
            preparedStatement.setString(3,student.getGender());
            preparedStatement.setInt(4,student.getAge());
            preparedStatement.setString(5,student.getClassName());
            preparedStatement.setInt(6,student.getId());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("修改成功");
            }else{
                System.out.println("修改失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return 0;
    }

    @Override
    public Student findstudentById(int id) {
        Student st =null;
        try (Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT * FROM student " +
                    "WHERE id= ? ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id1 = resultSet.getInt(1);
                int studentId = resultSet.getInt(2);
                String name= resultSet.getString(3);
                String gender= resultSet.getString(5);
                int age = resultSet.getInt(4);
                String className= resultSet.getString(6);
                st =  new Student(id1,studentId,name,gender,age,className);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  st;
    }

    @Override
    public Student findstudentByusernameAndPassword(String username, String password) {
        return null;
    }

    @Override
    public List<Student>  findAllstudent() {
        List<Student>  s  = new ArrayList<>();
        try (Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT * FROM student ;" ;
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id1 = resultSet.getInt(1);
                int studentId = resultSet.getInt(2);
                String name= resultSet.getString(3);
                String gender= resultSet.getString(5);
                int age = resultSet.getInt(4);
                String className= resultSet.getString(6);
                Student  st =  new Student(id1,studentId,name,gender,age,className);
                s.add(st);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  s;

    }
}
